//
//  CardDetailView.swift
//  TanTan
//
//  Created by 于航 on 2024/9/16.
//

import SwiftUI

struct CardDetailView: View {
    @EnvironmentObject var appState : AppState
    var nameSpace : Namespace.ID?
    var catCard : CatCard
    var body: some View {
        let screen = UIScreen.main.bounds
        ZStack(alignment : .bottom) {
            ScrollView(showsIndicators: false) {
                VStack(spacing: 0) {
                    CardView(catCard: catCard)
                        .animation(.easeOut(duration: 0.2), value: 1)
                        .matchedGeometryEffect(id: catCard.id, in: nameSpace ?? Namespace().wrappedValue)
                        .frame(width: screen.width, height: screen.height * 0.7)
                        .environmentObject(appState)
                    HStack {
                        VStack(alignment: .leading) {
                            HStack {
                                Text(catCard.name)
                                    .font(.system(size: 32, weight: .bold))
                                    .foregroundStyle(Color.accentColor)
                                Text(String(catCard.age) + "岁")
                                    .font(.system(size: 20, weight: .light))
                                Spacer()
                            }
                            HStack {
                                Text(catCard.category)
                                    .font(.system(size: 18, weight: .bold))
                                    .foregroundStyle(.gray)
                                Image(systemName: "location")
                                Text("\(catCard.address)")
                                    .font(.system(size: 15))
                            }
                        }
                        .padding([.horizontal, .top], 20)
                        Button(action: {
                            appState.isFullScreen = false
                        }, label: {
                            Image(systemName: "arrow.up.circle.fill")
                                .font(.system(size: 40))
                                .background(.white)
                                .clipShape(Circle())
                        })
                        .padding(.trailing, 30)
                        .offset(y: -40)
                    }
                    Spacer(minLength: 20)
                    Text(catCard.info)
                        .font(.system(size: 18, weight: .medium))
                        .foregroundStyle(.gray)
                        .multilineTextAlignment(.leading)
                        .padding(.horizontal, 20)
                    Spacer(minLength: 20)
                    VStack {
                        Button(action: {
                            showActionSheet()
                        }, label: {
                            VStack {
                                Text("分享 \(catCard.name)的信息")
                                    .font(.system(size: 16, weight: .medium))
                            }
                        })
                    }
                    Spacer(minLength: 200)
                }
            }
            HStack(spacing: 20){
                RoundButton(type: .no)
                RoundButton(type: .star)
                RoundButton(type: .heart)
            }
            .frame(height: 45)
            .frame(maxWidth: .infinity)
            .padding(.bottom, 45)
            .background(
                LinearGradient(gradient: Gradient(colors: [Color.white.opacity(0.2), Color.white]), startPoint: .top, endPoint: .bottom))
        }
        .edgesIgnoringSafeArea(.bottom)
    }
    
    func showActionSheet() {
        let items = ["What do you think about \(catCard.name)?"]
        let activity = UIActivityViewController(activityItems: items, applicationActivities: nil)
        if let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene {
            if let window = scene.windows.first(where: { $0.isKeyWindow }) {
                window.rootViewController?.present(activity, animated: true)
            }
        }
    }
}


#Preview {
    CardDetailView(nameSpace: nil, catCard :  CatCard(name: "猪猪", age: 1, address: "小区门口", category: "橘猫", photos: ["Cat10", "Cat11"], info: "橘猫是一种哺乳纲猫科动物，以其金黄色的被毛和特殊的颜色斑纹而闻名，因此也被称为橘子猫或桔猫。‌橘猫的全身为黄色，大多为短毛，有极少数是长毛，是中华田园猫中较为常见的毛色。身上的斑纹极像虎皮的纹路，所以又被称为虎斑猫。橘猫的体重比其他花色的公猫较重，性格友好，很容易被爱。‌"))
        .environmentObject(AppState(isFullScreen: true))
}
